package com.seanchenxi.gwt.wordpress.xmlrpc.service.impl;

import java.util.ArrayList;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.seanchenxi.gwt.wordpress.domain.model.Blog;
import com.seanchenxi.gwt.wordpress.domain.model.Post;
import com.seanchenxi.gwt.wordpress.domain.model.UserInfo;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.BloggerServiceAsync;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.constant.HasBloggerServiceMethodNames;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.BlogsResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.PostResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.PostsResponseHandler;
import com.seanchenxi.gwt.wordpress.xmlrpc.service.model.handler.UserInfosResponseHandler;
import com.seanchenxi.gwt.xmlrpc.client.XmlRpcClient;
import com.seanchenxi.gwt.xmlrpc.client.XmlRpcRequest;

/**
 * The Class BloggerServiceAsyncImpl.
 */
public class BloggerServiceAsyncImpl extends XmlRpcClient implements BloggerServiceAsync, HasBloggerServiceMethodNames {

	/**
	 * Instantiates a new blogger service async impl.
	 *
	 * @param url the url
	 */
	public BloggerServiceAsyncImpl(String url) {
		super(url);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.BloggerServiceAsync#getUsersBlogs(java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getUsersBlogs(String username, String password,
			AsyncCallback<ArrayList<Blog>> callback) {
		XmlRpcRequest<ArrayList<Blog>> xmlRpcRequest = createRequest(
				GET_USERS_BLOGS, username, password);
		send(xmlRpcRequest, callback, new BlogsResponseHandler());	
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.BloggerServiceAsync#getUserInfo(java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getUserInfo(String username, String password,
			AsyncCallback<ArrayList<UserInfo>> callback) {
		XmlRpcRequest<ArrayList<UserInfo>> xmlRpcRequest = createRequest(
				GET_USER_INFO, username, password);
		send(xmlRpcRequest, callback, new UserInfosResponseHandler());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.BloggerServiceAsync#getPost(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getPost(int postId, String username, String password,
			AsyncCallback<Post> callback) {
		XmlRpcRequest<Post> xmlRpcRequest = createRequest(
				GET_POST, postId, username, password);
		send(xmlRpcRequest, callback, new PostResponseHandler());
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.BloggerServiceAsync#getRecentPosts(int, java.lang.String, java.lang.String, int, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getRecentPosts(int blogId, String username, String password,
			int number, AsyncCallback<ArrayList<Post>> callback) {
		XmlRpcRequest<ArrayList<Post>> xmlRpcRequest = createRequest(
				GET_RECENT_POST, blogId, username, password, number);
		send(xmlRpcRequest, callback, new PostsResponseHandler());	
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.BloggerServiceAsync#getTemplate(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void getTemplate(int blogId, String username, String password,
			AsyncCallback<String> callback) {
		XmlRpcRequest<String> xmlRpcRequest = createRequest(
				GET_TEMPLATE, blogId, username, password);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.BloggerServiceAsync#setTemplate(int, java.lang.String, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void setTemplate(int blogId, String username, String password,
			String content, AsyncCallback<Boolean> callback) {
		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(
				SET_TEMPLATE, blogId, username, password, content);
		send(xmlRpcRequest, callback);
	}

	/* (non-Javadoc)
	 * @see com.seanchenxi.gwt.wordpress.service.BloggerServiceAsync#deletePost(int, java.lang.String, java.lang.String, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void deletePost(int postId, String username, String password,
			AsyncCallback<Boolean> callback) {
		XmlRpcRequest<Boolean> xmlRpcRequest = createRequest(
				DELETE_POST, postId, username, password);
		send(xmlRpcRequest, callback);
	}

}
